package com.koron.css2.meterService.impl;

import com.github.pagehelper.PageInfo;
import com.koron.css2.ImportExcelInterface;
import com.koron.css2.baseConfig.mapper.MeterBoreMapper;
import com.koron.css2.baseConfig.queryBean.MeterBoreQueryBean;
import com.koron.css2.baseConfig.vo.MeterBoreVO;
import com.koron.css2.common.dto.HeaderInfoDto;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class MsStorageNotStructImport implements ImportExcelInterface{
	@Override
	public MessageBean<?> importExel(SessionFactory factory, UserInfoBean userInfo, RequestBean req,List<Map<String, String>> data, MultipartFile file) {
		@SuppressWarnings("rawtypes")
		MessageBean<PageInfo> message = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "导入成功", PageInfo.class);
		MeterBoreMapper meterBoreMapper = factory.getMapper(MeterBoreMapper.class);
		// 水表口径
		List<MeterBoreVO> meterBores = meterBoreMapper.selectMeterBoreList(new MeterBoreQueryBean());
		List<String> errorList = new ArrayList<>();	//水表口径 错误列表
		List<Map<String, String>> factoryError = new ArrayList<>();//水表厂家 错误列表
		data.stream().forEach(t->{
			if (StringUtils.isNotBlank(t.get("meterBore"))) {
    			MeterBoreVO meterBore = meterBores.stream().filter(k -> Objects.equals(t.get("meterBore"), k.getBoreName())).findFirst().orElse(new MeterBoreVO());  			
    			
    			if(StringUtils.isEmpty(meterBore.getId()))
    				errorList.add(t.get("meterBore"));
    			else
    				t.put("meterBore",meterBore.getId());
    		}
		}); 
		
		factoryError = data.stream().filter(t->!Objects.equals(t.get("meterFactory"), data.get(0).get("meterFactory"))).collect(Collectors.toList());
		if(factoryError!=null && factoryError.size()>0)
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "导入的水表厂家不相同", null);
		if(errorList!=null && errorList.size()>0)
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "不存在水表口径:"+errorList.toString(), null);

		PageInfo<Map<String, String>> res = new PageInfo<>(data);
		res.setTotal(data.size());
		res.setPageNum(1);
		message.setData(res);
		return message;
	}

	@Override
	public List<HeaderInfoDto> getHeaderInfo(UserInfoBean userInfo) {
		List<HeaderInfoDto> headerInfos = new ArrayList<HeaderInfoDto>();
		headerInfos.add(new HeaderInfoDto("水表编号", true, "meterNo"));
		headerInfos.add(new HeaderInfoDto("水表型号", true, "meterModel"));
		headerInfos.add(new HeaderInfoDto("水表口径", true, "meterBore"));
		headerInfos.add(new HeaderInfoDto("水表表身号", true, "factoryNo"));
		headerInfos.add(new HeaderInfoDto("IMEI", false, "imei"));
		headerInfos.add(new HeaderInfoDto("IMSI", false, "imsi"));
		headerInfos.add(new HeaderInfoDto("合格证号", false, "meterCert"));
		
		return headerInfos;
	}

	@Override
	public boolean isCheckHeader() {
		return true;
	}

}
